<?php

namespace App\Module\Api\Controllers;
use App\Exceptions\Customize\ParamsException;
use App\Module\Api\Controllers\Request\AddressSubmitRequest;
use App\Module\Api\Validator\AddressSubmitValidator;
use App\System\Base\Services\TokenService;
use App\System\Cis\Repositories\CisUserAddressRepository;

/**
 *添加编辑收货地址
 */
class AddressSubmitController extends Controller
{

    public function __construct()
    {
        $this->query = new AddressSubmitRequest();
        parent::__construct();

    }

    public function index()
    {
        (new AddressSubmitValidator())->check();
        $master_id = TokenService::getMasterId();
        $address = $this->query;
        $user_address = CisUserAddressRepository::getInstance();
        $res = $user_address->editData($address,$master_id);
        if(!$res && !$address->id)
        {
            throw new ParamsException([
                'error_code' => '1000',
                'message'    => '添加失败',
            ]);
        }
        return apiResponse(['id'=>$res]);
    }
}
